package com.estar.assistsurvy.activity;

import java.util.ArrayList;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.adapter.MainPageViewpagerAdapter;
import com.estar.assistsurvy.fragment.GrabFragment;
import com.estar.assistsurvy.fragment.GrabPondFragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

public class RoborderActivity extends BaseActivity implements OnClickListener,OnCheckedChangeListener,OnPageChangeListener{
	private ArrayList<Fragment> fragments;
	private GrabPondFragment robbingFragment;
	private GrabFragment robbedFragment;
	private TextView robOrder_title;
	private LinearLayout returnBack;
	private RadioButton order_notRob,order_isRob;
	private ViewPager orderPager;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_roborder);
		initView();

		//		setData(Constants.ROBBING_ORDER);
		robOrder_title.setText(R.string.notOrder);
		FragmentManager manager=getSupportFragmentManager();
		MainPageViewpagerAdapter adapter=new MainPageViewpagerAdapter(manager, fragments);
		orderPager.setAdapter(adapter);
		orderPager.addOnPageChangeListener(this);
	}

	private void initView(){
		robOrder_title=find(R.id.robOrder_title);
		returnBack=find(R.id.returnBack);
		returnBack.setOnClickListener(this);
		order_isRob=find(R.id.order_isRob);
		order_isRob.setOnCheckedChangeListener(this);
		order_notRob=find(R.id.order_notRob);
		order_notRob.setOnCheckedChangeListener(this);
		orderPager=find(R.id.orderPager);

		fragments=new ArrayList<Fragment>();
		robbingFragment=new GrabPondFragment();
		fragments.add(robbingFragment);
		robbedFragment=new GrabFragment();
		fragments.add(robbedFragment);

	}

	//	public void setData(String orderType){
	//		if(orderType.equals(Constants.ROBBING_ORDER)){
	//			robOrder_title.setText(R.string.notOrder);
	//
	//		}else if(orderType.equals(Constants.ROBBING_ORDER)){
	//			robOrder_title.setText(R.string.isOrder);
	//		}
	//		
	//	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.returnBack:
			finish();
			break;

		default:
			break;
		}
	}


	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if(isChecked){
			switch (buttonView.getId()) {

			case R.id.order_notRob:
				clearState();
				orderPager.setCurrentItem(0);
				order_notRob.setChecked(true);
				order_notRob.setTextColor(getResources().getColor(R.color.colorBgWhite));
				robOrder_title.setText(R.string.notOrder);
				break;

			case R.id.order_isRob:
				clearState();
				orderPager.setCurrentItem(1);
				order_isRob.setChecked(true);
				order_isRob.setTextColor(getResources().getColor(R.color.colorBgWhite));
				robOrder_title.setText(R.string.isOrder);
				break;

			default:
				break;
			}
		}
	}

	private void clearState(){
		order_isRob.setChecked(false);
		order_isRob.setTextColor(getResources().getColor(R.color.colorTextOrange));
		order_notRob.setChecked(false);
		order_notRob.setTextColor(getResources().getColor(R.color.colorTextOrange));
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int position) {
		// TODO Auto-generated method stub
		switch (position) {
		case 0:
			clearState();
			order_notRob.setChecked(true);
			order_notRob.setTextColor(getResources().getColor(R.color.colorBgWhite));
			robOrder_title.setText(R.string.notOrder);
			break;

		case 1:
			clearState();
			order_isRob.setChecked(true);
			order_isRob.setTextColor(getResources().getColor(R.color.colorBgWhite));
			robOrder_title.setText(R.string.isOrder);
			break;

		default:
			break;
		}
	}
}
